package repository;

import javax.jdo.PersistenceManager;

public class PersistenceManagerUtil {

	private static ThreadLocal<PersistenceManager> PERSISTENCE_MANAGER_CACHE = new ThreadLocal<PersistenceManager>();
	
	public static PersistenceManager getPersistenceManager() {
		PersistenceManager pm = PERSISTENCE_MANAGER_CACHE.get();
		if (pm == null || pm.isClosed()) {
			pm = PMF.get().getPersistenceManager();
			PERSISTENCE_MANAGER_CACHE.set(pm);
		}
		return pm;
	}
	
	public static void closePersistenceManager() {
		PersistenceManager pm = PERSISTENCE_MANAGER_CACHE.get();
		pm.close();
		PERSISTENCE_MANAGER_CACHE.set(null);
	}
}
